<template>
  <!--添加表单-->
  <div class="login_container">
    <!--添加表单-->
    <div class="login_box">
      <!--添加表单-->
      <div class="avatar_box">
        <!--头像-->
        <img src="../assets/logo.png" alt />
      </div>
      <!--添加表单-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login_form"
        label-width="0px"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icondenglu"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont iconmima" type="password"></el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-row :gutter="18">
            <el-col :span="13">
              <el-input v-model="loginForm.captcha" placeholder="验证码">
              </el-input>
            </el-col>
            <el-col :span="10" class="login-captcha">
              <img :src="captchaPath" @click="getCaptcha()" alt="" title="看不清？点击切换">
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { JSEncrypt } from 'jsencrypt'
export default {
  data() {
    return {
      public_key: '',
      captchaPath: '',
      //表单默认展示的用户名和密码
      loginForm: {
        username: '',
        password: '',
        captcha:'',
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },//必填项验证，离焦验证
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }//长度验证，离焦验证
        ],
        //校验密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },  //必填项验证，离焦验证
          { min: 6, max: 8, message: "密码为 6~8 位", trigger: "blur" } //长度验证，离焦验证
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
	var that = this
    this.getCaptcha()	
	console.log(window.location,':window.location.search')
	var search = window.location.href.split('?')[1];
	// 获取admin参数的值  
	console.log("search",search)//loginName=1
	if(search){
		var param = search.split('=');
		console.log("loginName",param[1])
		//登录+跳转
		that.$router.replace({ path: 'popInfo' })
	}
  },
  mounted() {
    //请求数据
    //页面加载获取公钥
    this.getPkey();
  },
  methods: {
    async getPkey(){
      // 调用post请求
      const {data :res} = await this.$http.get('/getPkey'); //访问后台
      if(res.success){
        this.public_key = res.data;
      }
    },
    //重置表单内容
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    //登陆方法
    login() {
      var that = this
      //1.验证校验规则ss
      var encrypt = new JSEncrypt();
      encrypt.setPublicKey(this.public_key);
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return; //验证失败
        // 调用post请求
        //const {data :res} = await this.$http.post("/login",this.loginForm); //访问后台
        const {data :res} = await this.$http.post("/login", {
          username:that.loginForm.username,
          password:encrypt.encrypt(that.loginForm.password),
          captcha:that.loginForm.captcha,
        });
        if (res.success) {
           this.$message.success("登陆成功！！！"); //信息提示
           window.sessionStorage.setItem("user",JSON.stringify(res.data)); // 存储user对象
           this.$router.push({ path: "/home"}); //页面跳转
         }else{
          this.$message.error("登录失败！！！"); //错误提示
         }
      });
    },
    // 获取验证码
    async getCaptcha () {
      const { data: res } = await this.$http.get('/getCaptcha/?number=' + Math.random())
      this.captchaPath = res // 验证码图片渲染
    }
  },
};
</script>

<style lang="less" scoped>

// 跟节点样式
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
// 登录框
.login_box {
  width: 450px;
  height: 340px;
  background-color: #fff;
  border-radius: 3px;// 圆角
  position: absolute;// 绝对定位
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);// 根据自己位置 以自己为长度位移
  // 头像框
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%; // 加圆角
    padding: 10px;
    box-shadow: 0 0 10px #ddd;// 盒子阴影
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #0ee;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%; // 加圆角
      background-color: #eee;
    }
  }
  .btns {
    display: flex;// 弹性布局
    justify-content: flex-end; // 尾部对齐
  }
  .login_form {
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;// 设置边框
  }
}
</style>